﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class SO : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument SalesInvoice;
        private ReportDocument SalesInvoiceDetail;
        private string Tipe;

        public SO()
        {
            DA = new DataAccess<object>();
            SalesInvoice = new ReportDocument();
            SalesInvoiceDetail = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            var query = Server.UrlDecode(Request.QueryString[0].ToString());
            if (IsPostBack)
                if (RadioButtonList2.SelectedValue.ToString().Contains("Summary"))
                    LoadReportSummary(query, RadioButtonList1.SelectedValue);
                else
                    LoadReportDetail(query, RadioButtonList1.SelectedValue);
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (RadioButtonList2.SelectedValue.ToString().Contains("Summary"))
                LoadReportSummary(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
            else
                LoadReportDetail(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }


        public void LoadReportSummary(string  NoDO,string tipe)
        {

            string sql = "";
            sql =
                "Select a.SoNo as SINo,a.SODate as InvoiceDate,a.Term,a.Rate,a.Ppn,a.Keterangan,a.Discount,a.CurrencyCode, " +
                "(Case When a.DiscountType is Null then '' Else a.DiscountType End) as DiscountType  , " +
                "b.Qty,b.Price,b.Discount as DiscountD,(Case When b.DiscountType is Null then '' Else b.DiscountType End) as DiscountTypeD, " +
                "c.CustomerName,c.Address,c.ContactPerson,c.Telp,c.Fax,i.*,item.ItemName,s.SubgroupName,sa.NamaDepan as KodeSalesman  " +
                "from SalesOrder a inner Join SalesOrderDetail b on a.SONO = b.SONO  " +
                "Left Join Customer c on c.CustomerId = a.CustomerId  Left Join Inventory i on i.SerialNumber = b.SerialNumber  " +
                "Left Join Item item on item.ItemId = i.ItemId Left Join Subgroup s on s.SubGroupID = item.SubgroupId  " +
                "Left Join Salesman sa on sa.KodeSalesman = a.KodeSalesman  " +
                "Where a.SONO='" + NoDO + "'  Order By s.SubgroupId,i.ItemId ";
            

            var Source = DA.GetDataListDataSet(sql);
            string reportPath = Server.MapPath("~/Report/SalesOrderNew.rpt");

            SalesInvoice.Load(reportPath);
            SalesInvoice.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            SalesInvoice.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = SalesInvoice;
            CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        }

        public void LoadReportDetail(string NoDO,string tipe)
        {
            string sql;

            sql =
                "Select a.SONO as SINo,a.SODate as InvoiceDate,a.Term,a.Rate,a.Ppn,a.Keterangan,a.Discount,a.CurrencyCode,  " +
                "(Case When a.DiscountType is Null then '' Else a.DiscountType End) as DiscountType  ,  " +
                "b.Panjang,b.Lebar,b.Tinggi,b.Qty,b.Price,b.Discount as DiscountD,  " +
                "(Case When b.DiscountType is Null then '' Else b.DiscountType End) as DiscountTypeD, c.CustomerName,  " +
                "c.Address,c.ContactPerson,c.Telp,c.Fax,i.NoBundle,i.SerialNumber,i.ItemId,i.SatuanUnit,item.ItemName,  " +
                "s.SubgroupName,sa.NamaDepan as KodeSalesman   " +
                "from SalesOrder a inner Join SalesOrderDetail b on a.SONO = b.SONO   " +
                "Left Join Customer c on c.CustomerId = a.CustomerId    " +
                "Left Join Inventory i on i.SerialNumber = b.SerialNumber   " +
                "Left Join Item item on item.ItemId = i.ItemId   " +
                "Left Join Subgroup s on s.SubGroupID = item.SubgroupId   " +
                "Left Join Salesman sa on sa.KodeSalesman = a.KodeSalesman  " +
                "Where a.SONO='" + NoDO + "'  Order By s.SubgroupId,i.ItemId ";

            var Source = DA.GetDataListDataSet(sql);
            string reportPath = Server.MapPath("~/Report/SalesOrderDetailNew.rpt");
            SalesInvoiceDetail.Load(reportPath);
            SalesInvoiceDetail.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            SalesInvoiceDetail.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = SalesInvoiceDetail;
            CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            SalesInvoice.Clone();
            SalesInvoice.Dispose();

            SalesInvoiceDetail.Clone();
            SalesInvoiceDetail.Dispose();
        }
    }
}